package com.lc.tgxm.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.PopupWindow;

import com.lc.tgxm.BaseApplication;
import com.lc.tgxm.R;
import com.lc.tgxm.activity.MyWebviewActivity;
import com.lc.tgxm.widget.ViewTitle;
import com.zcx.helper.fragment.AppV4Fragment;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

/**
 * BaseFragment Fragment基类
 * Created by Lei on 2017/01/19.
 */
public class BaseFragment extends AppV4Fragment implements View.OnClickListener {
    protected String TAG = getClass().getSimpleName();
    protected View rootView;
    protected ViewTitle viewTitle;
    protected int totalPage;// 总页数
    protected int currentIndex = 1;// 当前页数

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        rootView = getView();
    }

    protected void startActivityPutExtra(Class<?> cls, String name, int value) {
        Intent intent = new Intent(getActivity(), cls);
        intent.putExtra(name, value);
        startActivity(intent);
    }

    /**
     * 设置标题
     *
     * @param resid 标题文字(e.g., R.string.title)
     */
    protected void setTitleName(int resid) {
        viewTitle = (ViewTitle) rootView.findViewById(R.id.titlebar);
        viewTitle.setTitleName(resid);
    }

    /**
     * 设置标题
     *
     * @param name 标题文字(String类型)
     */
    protected void setTitleName(String name) {
        viewTitle = (ViewTitle) rootView.findViewById(R.id.titlebar);
        viewTitle.setTitleName(name);
    }

    @Override
    public void onClick(View view) {
    }

    protected void setBtnOnClick(int... resid) {
        for (int aResid : resid) {
            rootView.findViewById(aResid).setOnClickListener(this);
        }
    }

    /**
     * 初始化PopupWindow
     *
     * @param resource ID for an XML layout resource to load (e.g., R.layout.main_page)
     * @return PopupWindow
     */
    protected PopupWindow initPopWindow(int resource) {
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View pView = inflater.inflate(resource, null);
        ScaleScreenHelperFactory.getInstance().loadViewGroup((ViewGroup) pView);

        PopupWindow pw = new PopupWindow(pView);
        pw.setFocusable(true);
        pw.setOutsideTouchable(true);
        // 这个是为了点击“返回Back”也能使其消失，并且并不会影响你的背景
        pw.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        pw.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                blurredBackground(1.0f);
            }
        });
        return pw;
    }

    /**
     * 初始化 底部弹出的宽高
     *
     * @param resource ID for an XML layout resource to load (e.g., R.layout.main_page)
     * @return PopupWindow
     */
    protected PopupWindow initPopWindowFromBottom(int resource) {
        PopupWindow pw = initPopWindow(resource);
        pw.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        pw.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        return pw;
    }

    /**
     * 从屏幕底部弹出PopupWindow
     */
    protected void showPwFromBottom(PopupWindow pw) {
        if (pw == null)
            return;
        pw.setAnimationStyle(R.style.PopupAnimation);
        pw.showAtLocation(getActivity().getWindow().getDecorView(), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);

        // 背景变暗
        blurredBackground(0.7f);
    }

    /**
     * 背景变暗
     *
     * @param alpha 0.7f变暗;1.0f回复原状
     */
    protected void blurredBackground(float alpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = alpha;
        getActivity().getWindow().setAttributes(lp);
    }

    /**
     * 获取屏幕宽高
     *
     * @return int[0] width(宽度);int[1] height(高度)
     */
    protected int[] getDisplayWidthHeight() {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return new int[]{displaymetrics.widthPixels, displaymetrics.heightPixels};
    }

    @Override
    protected int layoutId() {
        return 0;
    }

    protected String getUserId() {
        return BaseApplication.basePreferences.getUserId();
    }

    protected boolean checkEmpty(String... strs) {
        for (String name : strs) {
            if (TextUtils.isEmpty(name)) {
                return true;
            }
        }

        return false;
    }

    @SuppressLint("SetJavaScriptEnabled")
    protected void initWebView(WebView wv, String url) {
        if (!TextUtils.isEmpty(url)) {
            WebSettings settings = wv.getSettings();
            settings.setJavaScriptEnabled(true);
            settings.setLoadWithOverviewMode(true);// 适应屏幕
            wv.setWebViewClient(new WebViewClient());
            wv.loadUrl(url);
        }
    }

    protected void startWebviewActivity(String title, String url) {
        Intent intent = new Intent(getActivity(), MyWebviewActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("url", url);
        startActivity(intent);
    }
}